<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productId">
<!--        :popper-append-to-body="false" popper-class="selectClass"-->
        <el-select v-model="queryParams.productId" placeholder="请选择产品信息" clearable filterable>
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.productName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跌涨类型" prop="biliType">
        <el-select v-model="queryParams.biliType" placeholder="请选择跌涨类型" clearable>
          <el-option
            v-for="dict in dict.type.bili_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['funct:pdetails:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['funct:pdetails:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['funct:pdetails:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['funct:pdetails:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pdetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="productId" :formatter="tranProductName"/>
      <el-table-column label="行情价格(元)" align="center" prop="productPrice" />
      <el-table-column label="跌涨价(元)" align="center" prop="biliPrice" >
        <template slot-scope="scope">
          <i v-if="scope.row.biliType == '0'" class="el-icon-minus" style="color: #7171C6"></i>
          <i v-if="scope.row.biliType == '1'" class="el-icon-top" style="color: green"></i>
          <i v-if="scope.row.biliType == '2'" class="el-icon-bottom" style="color: red"></i>
          <span>{{ scope.row.biliPrice }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="跌涨类型" align="center" prop="biliType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bili_type" :value="scope.row.biliType"/>
        </template>
      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注信息" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['funct:pdetails:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['funct:pdetails:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改价格明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="产品名称" prop="productId">
          <el-select v-model="form.productId" placeholder="请选择产品信息" clearable filterable>
            <el-option
              v-for="item in productList"
              :key="item.id"
              :label="item.productName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="行情价格(元)" prop="productPrice">
          <el-input v-model="form.productPrice" show-word-limit maxlength="12" placeholder="请输入行情价格" />
        </el-form-item>
        <el-form-item label="批发价格(元)" prop="guidePrice">
          <el-input v-model="form.guidePrice" show-word-limit maxlength="12" placeholder="请输入批发价格" />
        </el-form-item>
        <el-form-item label="指导价格(元)" prop="tradePrice">
          <el-input v-model="form.tradePrice" show-word-limit maxlength="12" placeholder="请输入指导价格" />
        </el-form-item>
        <el-form-item label="跌涨价(元)" prop="biliPrice">
          <el-input v-model="form.biliPrice" show-word-limit maxlength="12" placeholder="请输入跌涨价" />
        </el-form-item>
        <el-form-item label="跌涨类型" prop="biliType">
          <el-select v-model="form.biliType" placeholder="请选择跌涨类型">
            <el-option
              v-for="dict in dict.type.bili_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" show-word-limit type="textarea" rows="3" maxlength="1000" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPdetails, getPdetails, delPdetails, addPdetails, updatePdetails } from "@/api/funct/pdetails";
import { listProductAll } from "@/api/funct/product";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Pdetails",
  dicts: ['bili_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 价格明细表格数据
      pdetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        productPrice: null,
        guidePrice: null,
        tradePrice: null,
        biliPrice: null,
        biliType: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productPrice: {
          type: "string",
          trigger: "blur",
          pattern: '^\\d+(.\\d{1,2})?$',
          required: true,
          message: '行情价格需要符合正整数或最多保留2位小数!'
        },
        guidePrice: {
          type: "string",
          trigger: "blur",
          pattern: '^\\d+(.\\d{1,2})?$',
          required: false,
          message: '批发价格需要符合正整数或最多保留2位小数!'
        },
        tradePrice: {
          type: "string",
          trigger: "blur",
          pattern: '^\\d+(.\\d{1,2})?$',
          required: false,
          message: '指导价格需要符合正整数或最多保留2位小数!'
        },
        productId: {type: "number", trigger: "blur", required: true, message: '产品所属大类不能为空!'},
        biliPrice: {type: "string", trigger: "blur", required: true, message: '跌涨幅不能为空!'},
        biliType: {type: "string", trigger: "blur", required: true, message: '跌涨类型不能为空!'}
      },
      productList: []
    };
  },
  created() {
    this.getProductList();
    this.getList();
  },
  methods: {
    parseTime,
    // 获取所有产品信息
    getProductList() {
      this.loading = true;
      listProductAll().then(res => {
        this.productList = res.rows
        this.loading = false;
      })
    },
    /** 查询价格明细列表 */
    getList() {
      this.loading = true;
      listPdetails(this.queryParams).then(response => {
        this.pdetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 转换产品名称
    tranProductName(row, column, cellValue) {
      if (this.productList != null && this.productList != undefined && this.productList != undefined) {
        for (let i = 0; i < this.productList.length; i++) {
          if (this.productList[i].id == cellValue) {
            return this.productList[i].productName;
          }
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        productPrice: null,
        guidePrice: null,
        tradePrice: null,
        biliPrice: null,
        biliType: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加价格明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPdetails(id).then(response => {
        this.form = response.data;
        this.setOtherTypeToString();
        this.open = true;
        this.title = "修改价格明细";
      });
    },
    setOtherTypeToString() {
      this.$set(this.form, 'productPrice', this.form.productPrice != null ? this.form.productPrice.toString() : '');
      this.$set(this.form, 'guidePrice', this.form.guidePrice != null ? this.form.guidePrice.toString() : '');
      this.$set(this.form, 'tradePrice', this.form.tradePrice != null ? this.form.tradePrice.toString() : '');
      // this.$set(this.form, 'categoryId', this.form.categoryId.toString());
      // this.$set(this.form, 'subclassId', this.form.subclassId.toString());
      this.$set(this.form, 'biliPrice', this.form.biliPrice != null ? this.form.biliPrice.toString() : '');
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePdetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPdetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除价格明细编号为"' + ids + '"的数据项？').then(function() {
        return delPdetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('funct/pdetails/export', {
        ...this.queryParams
      }, `pdetails_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.selectClass {
  display: inline-block;
  width: 100%;
  overflow: initial;
}
</style>
